using MediatR;
using Microsoft.AspNetCore.Mvc;
using Medical.ErrorCode;
using BaseData.ErrorCode;
using Medical.ReadApi.Dto;
using Medical.WriteApi.Appcontext.Command;
using Medical.WriteApi.Dto;

namespace Medical.WriteApi.Controllers
{
	/// <summary>
	/// 科室管理控制器（写操作）
	/// </summary>
	[ApiController]
	[Route("api/[controller]/[action]")]
	public class DepartmentManagementController : ControllerBase
	{
		private readonly IMediator _mediator;
		
		/// <summary>
		/// 构造函数
		/// </summary>
		/// <param name="mediator">MediatR中介者</param>
		public DepartmentManagementController(IMediator mediator)
		{
			_mediator = mediator;
		}

		/// <summary>
		/// 创建科室
		/// </summary>
		/// <param name="command">创建科室命令，包含科室名称、地址、挂号费等信息</param>
		/// <returns>创建成功的科室信息</returns>
		/// <remarks>
		/// 示例请求:
		/// POST /api/DepartmentManagement/Create
		/// {
		///   "name": "内科",
		///   "address": "一楼A区",
		///   "registrationFee": 50.00,
		///   "parentDepartmentId": null,
		///   "isKeyDepartment": true,
		///   "isEmergencyAvailable": true,
		///   "bedCount": 100
		/// }
		/// </remarks>
		/// <response code="200">创建成功，返回科室信息</response>
		/// <response code="400">请求参数错误</response>
		/// <response code="500">服务器内部错误</response>
		[HttpPost]
		[ProducesResponseType(typeof(ApiResult<DepartmentDto>), 200)]
		[ProducesResponseType(400)]
		[ProducesResponseType(500)]
		public async Task<ApiResult<DepartmentDto>> Create([FromBody] CreateDepartmentCommand command)
		{
			try { var data = await _mediator.Send(command); return new ApiResult<DepartmentDto> { Code = ApiEnum.新增成功, Message = "创建成功", data = data }; }
			catch (Exception ex) { return new ApiResult<DepartmentDto> { Code = ApiEnum.新增失败, Message = ex.Message, data = null }; }
		}

		/// <summary>
		/// 更新科室信息
		/// </summary>
		/// <param name="command">更新科室命令，包含科室ID和要更新的字段</param>
		/// <returns>更新后的科室信息</returns>
		/// <remarks>
		/// 示例请求:
		/// PUT /api/DepartmentManagement/Update
		/// {
		///   "departmentId": 1,
		///   "name": "新科室名",
		///   "address": "新地址",
		///   "registrationFee": 60.00
		/// }
		/// </remarks>
		/// <response code="200">更新成功，返回更新后的科室信息</response>
		/// <response code="400">请求参数错误</response>
		/// <response code="404">科室不存在</response>
		/// <response code="500">服务器内部错误</response>
		[HttpPut]
		[ProducesResponseType(typeof(ApiResult<DepartmentDto>), 200)]
		[ProducesResponseType(400)]
		[ProducesResponseType(404)]
		[ProducesResponseType(500)]
		public async Task<ApiResult<DepartmentDto>> Update([FromBody] UpdateDepartmentCommand command)
		{
			try { var data = await _mediator.Send(command); return new ApiResult<DepartmentDto> { Code = ApiEnum.更新成功, Message = "更新成功", data = data }; }
			catch (Exception ex) { return new ApiResult<DepartmentDto> { Code = ApiEnum.更新失败, Message = ex.Message, data = null }; }
		}

		/// <summary>
		/// 删除科室（逻辑删除）
		/// </summary>
		/// <param name="departmentId">科室ID</param>
		/// <param name="deletedBy">删除操作人（可选）</param>
		/// <returns>删除操作是否成功</returns>
		/// <remarks>
		/// 示例请求:
		/// DELETE /api/DepartmentManagement/Delete?departmentId=1&amp;deletedBy=admin
		/// 
		/// 注意：此操作是逻辑删除，不会真正删除数据库中的记录。
		/// </remarks>
		/// <response code="200">删除成功</response>
		/// <response code="400">请求参数错误</response>
		/// <response code="404">科室不存在</response>
		/// <response code="500">服务器内部错误</response>
		[HttpDelete]
		[ProducesResponseType(typeof(ApiResult<bool>), 200)]
		[ProducesResponseType(400)]
		[ProducesResponseType(404)]
		[ProducesResponseType(500)]
		public async Task<ApiResult<bool>> Delete([FromQuery] long departmentId, [FromQuery] string? deletedBy = null)
		{
			try { var ok = await _mediator.Send(new DeleteDepartmentCommand { DepartmentId = departmentId, DeletedBy = deletedBy }); return new ApiResult<bool> { Code = ApiEnum.删除成功, Message = "删除成功", data = ok }; }
			catch (Exception ex) { return new ApiResult<bool> { Code = ApiEnum.删除失败, Message = ex.Message, data = false }; }
		}

		/// <summary>
		/// 为科室添加疾病
		/// </summary>
		/// <param name="command">创建疾病命令，包含科室ID和疾病名称</param>
		/// <returns>创建成功的疾病信息</returns>
		/// <remarks>
		/// 示例请求:
		/// POST /api/DepartmentManagement/AddDisease
		/// {
		///   "departmentId": 1,
		///   "name": "感冒"
		/// }
		/// </remarks>
		/// <response code="200">创建成功，返回疾病信息</response>
		/// <response code="400">请求参数错误</response>
		/// <response code="404">科室不存在</response>
		/// <response code="500">服务器内部错误</response>
		[HttpPost]
		[ProducesResponseType(typeof(ApiResult<DiseaseDto>), 200)]
		[ProducesResponseType(400)]
		[ProducesResponseType(404)]
		[ProducesResponseType(500)]
		public async Task<ApiResult<DiseaseDto>> AddDisease([FromBody] CreateDiseaseCommand command)
		{
			try { var data = await _mediator.Send(command); return new ApiResult<DiseaseDto> { Code = ApiEnum.新增成功, Message = "新增成功", data = data }; }
			catch (Exception ex) { return new ApiResult<DiseaseDto> { Code = ApiEnum.新增失败, Message = ex.Message, data = null }; }
		}

		/// <summary>
		/// 删除疾病（逻辑删除）
		/// </summary>
		/// <param name="diseaseId">疾病ID</param>
		/// <param name="deletedBy">删除操作人（可选）</param>
		/// <returns>删除操作是否成功</returns>
		/// <remarks>
		/// 示例请求:
		/// DELETE /api/DepartmentManagement/RemoveDisease?diseaseId=1&amp;deletedBy=admin
		/// 
		/// 注意：此操作是逻辑删除，不会真正删除数据库中的记录。
		/// </remarks>
		/// <response code="200">删除成功</response>
		/// <response code="400">请求参数错误</response>
		/// <response code="404">疾病不存在</response>
		/// <response code="500">服务器内部错误</response>
		[HttpDelete]
		[ProducesResponseType(typeof(ApiResult<bool>), 200)]
		[ProducesResponseType(400)]
		[ProducesResponseType(404)]
		[ProducesResponseType(500)]
		public async Task<ApiResult<bool>> RemoveDisease([FromQuery] long diseaseId, [FromQuery] string? deletedBy = null)
		{
			try { var ok = await _mediator.Send(new DeleteDiseaseCommand { DiseaseId = diseaseId, DeletedBy = deletedBy }); return new ApiResult<bool> { Code = ApiEnum.删除成功, Message = "删除成功", data = ok }; }
			catch (Exception ex) { return new ApiResult<bool> { Code = ApiEnum.删除失败, Message = ex.Message, data = false }; }
		}
	}
}
